"""
比get_users更精确,可以获取到三个层级的员工信息 formal  trial   intern
"""
from flask_login import current_user
from app.user.models import Account
from utils import current_company
from utils.perm import e


class Users():
    """
    可以获得四种状态的所有人的list:
        1.包含老板==>所有成员的list             Users().get_related_users(is_all=True)
        2.不包含老板==>所有成员的list           Users().users_exclude_manager(is_all=True)
        3.包含老板==>登录成员同级成员的list     Users().get_related_users(staff_status="同级员工状态")
        4.不包含老板==>登录成员同级成员的list   Users().users_exclude_manager(staff_status="同级员工状态")
        5.支持额外增加多个参数的自定制查询:     **kwargs   自己可以添加多个参数来进一步查询出在上面条件下的users
        6.通过字段is_common_department可以快速查询出相同部门下的所有人的obj的list(包括自己) 可以通过限制条件来限制自己,不查出自己
        7.通过字段actions=[("insight","operate"),......]来选出有特定动作权限的user对象来   例子:Users().get_related_users(actions=[("insight", "operate")])
    """

    def __init__(self):
        pass

    def common_department(self, users):
        # 当前用户所在的部门list
        user_group = [item[0] for item in current_user.departments]
        users_tmp = []
        for item in users:
            # 得到每个对应用户的部门list
            temp_user_departments_list = []
            for item1 in item.departments:
                temp_user_departments_list.append(item1[0])
            if set(temp_user_departments_list) & set(user_group):
                users_tmp.append(item)
        return users_tmp

    def get_related_users(self, actions=None, staff_status="formal", is_all=False, is_common_department=False,
                          **kwargs):
        """
        得到当前登录用户对应的成员的所有list
        :param staff_status:
        :is_all:是否返回所有在职员工的数据,默认为False
        :staff_status:当前员工的状态,默认为正式员工
        :return:
        """
        if is_all:
            users = Account.objects(is_resigned=False, **kwargs)
            if is_common_department:
                users = self.common_department(users)
        else:
            if staff_status == 'intern':  # 实习生
                users = Account.objects(is_resigned=False, staff_status="intern", **kwargs)
                if is_common_department:
                    users = self.common_department(users)
            elif staff_status == "trial":  # 试用期
                users = Account.objects(is_resigned=False, staff_status="trial", **kwargs)
                if is_common_department:
                    users = self.common_department(users)
            else:  # 本公司的正式员工
                users = Account.objects(is_xxxer=True, is_resigned=False, staff_status__not__in=["intern", "trial"],
                                        **kwargs)
                if is_common_department:
                    users = self.common_department(users)

        if actions:
            user_list = []
            for user_obj in users:
                has_perm = True in [e.enforce(user_obj.unionid, content, action, current_company.name) for
                                    content, action in actions]
                if has_perm:
                    user_list.append(user_obj)
            return user_list
        else:
            return users

    def users_exclude_manager(self, actions=None, staff_status="formal", is_all=False, is_common_department=False,
                              **kwargs):
        """
        根据当前员工状态选择出排除manager之外的所有未辞退的员工对象list
        :param staff_status:默认为正式员工
        :return:
        """
        if is_all:
            users = self.get_related_users(staff_status=staff_status, actions=actions, is_all=True,
                                           is_common_department=is_common_department,
                                           **kwargs)

        else:
            users = self.get_related_users(staff_status=staff_status, actions=actions,
                                           is_common_department=is_common_department, **kwargs)

        user_list = [(item, item.departments) for item in users]
        user_list_ultimate = []
        for item in user_list:
            for item1 in item[1]:
                if item1[0] == "manager":
                    pass
                else:
                    user_list_ultimate.append(item[0])

        return user_list_ultimate
